package top.infopub.mgr.fia.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.fia.dao.FeeNoteDao;
import top.infopub.mgr.fia.domain.FeeNoteDomain;
import top.infopub.mgr.fia.domain.FeeNotesDomain;
import top.infopub.mgr.fia.service.FeeNoteService;
import top.infopub.mgr.fia.vo.FeeNoteVo;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.fia.FeeNote;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;

@Service
public class FeeNoteServiceImpl extends GenericServiceImpl<FeeNote, String> implements
		FeeNoteService {

	@Autowired
	private FeeNoteDao feeNoteDao;
	
	@Override
	public GenericDao<FeeNote, String> getDao() {
		return feeNoteDao;
	}

	@Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;
	
    @Override
    public Page<FeeNoteDomain> selectByDomainWhere(Page<FeeNoteDomain> page, FeeNoteVo feeNoteVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Page<FeeNoteDomain> lan = feeNoteDao.selectByDomainWhere(feeNoteVo);
        return lan;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveFeeNote(FeeNote feeNote) {
        
        String codes = codeGeneratorConfigService.generatorCode("fia_fee_note"); //单据编号
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        
        feeNote.setId(IdFactory.getUUIDUpper());
        feeNote.setCodes(codes);
        feeNote.setAddBy(vo.getId());
        feeNote.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        feeNote.setUptBy(vo.getId());
        feeNote.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        feeNote.setState(1); //已登记
        feeNoteDao.insert(feeNote);
    }

    //根据费用归属id查询公司
    @Override
    public String getHrOrgBillName(String billId) {

        String billName = feeNoteDao.getHrOrgBillNameByBillId(billId);
        return billName;
    }

    @Override
    public Boolean changeState(String id, int state) {

        return feeNoteDao.changeState(id,state);
    }

    @Override
    public FeeNoteDomain queryDomainById(String id) {
        return feeNoteDao.queryDomainById(id);
    }

    @Override
    public Page<FeeNotesDomain> selectDataByWhere1(Date monthStart, Date monthEnd, String type,
                                                   String orgId, Page<FeeNotesDomain> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Map<String, Object> where = Maps.newHashMap();
        where.put("stsMonthStart", monthStart);
        Date stsMonthEnd = DateUtils.getMonthEnd(monthEnd);
        where.put("stsMonthEnd", stsMonthEnd);       
        where.put("hrOrgBillId", orgId);
        where.put("noteType", '2');//管理费用类型
        return (Page<FeeNotesDomain>)feeNoteDao.selectDataByWhere(where);
    }

    @Override
    public Page<FeeNotesDomain> selectDataByWhere2(Date monthStart, Date monthEnd, String type,
                                                   String orgId, Page<FeeNotesDomain> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Map<String, Object> where = Maps.newHashMap();
        where.put("stsMonthStart", monthStart);
        Date stsMonthEnd = DateUtils.getMonthEnd(monthEnd);
        where.put("stsMonthEnd", stsMonthEnd);       
        where.put("hrOrgBillId", orgId);
        where.put("noteType", '3');//营销费用类型
        return (Page<FeeNotesDomain>)feeNoteDao.selectDataByWhere(where);
    }

}